import Euclid.*;
import java.math.*;

public class EuclidExport implements EuclidExporter {

	public String[] getExports()
	{
		return new String[] { "gcd", "modinv" };
	}
	
	public VarType gcd(VarType a, VarType b)
	{
		if (a instanceof SymbVarType)
			return gcd(((SymbVarType)a).val, b);
		else if (b instanceof SymbVarType)
			return gcd(a, ((SymbVarType)b).val);
		else if ((a instanceof IntVarType) && (b instanceof IntVarType))
			return new IntVarType(GCD.Euclidean(((IntVarType)a).val, ((IntVarType)b).val),
					"\\mathrm{gcd}(" + a.formula + ", " + b.formula + ")");
		else
			return null;
	}
	
	public VarType modinv(VarType a, VarType b)
	{
		if (a instanceof SymbVarType)
			return modinv(((SymbVarType)a).val, b);
		else if (b instanceof SymbVarType)
			return modinv(a, ((SymbVarType)b).val);
		else if ((a instanceof IntVarType) && (b instanceof IntVarType))
		{
			/*
			BigInteger res[] = GCD.ExtEuclidean(((IntVarType)a).val, ((IntVarType)b).val);
			if (res[2].compareTo(BigInteger.ONE) != 0)
				return null;  // no mod inverse
			else {
				return new IntVarType(res[0],
					a.formula + "^{-1}(\\mathrm{mod} \\nbsp " + b.formula + ")");
			}
			*/
			return new IntVarType(((IntVarType)a).val.modInverse(((IntVarType)b).val),
					a.formula + "^{-1}(\\mathrm{mod} \\nbsp " + b.formula + ")");
		}
		else
			return null;
	}
}
